package com.ktgj.customer.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.ktgj.common.core.constant.FlightOrderConstants;
import com.ktgj.customer.domain.Employee;
import com.ktgj.customer.domain.FinanceAutobillRecord;
import com.ktgj.customer.domain.vo.AutobillExceptionVo;
import com.ktgj.customer.domain.vo.BackfillTicketNumberVo;
import com.ktgj.customer.domain.vo.FinanceAutobillExceptionVo;
import com.ktgj.customer.mapper.EmployeeMapper;
import com.ktgj.customer.mapper.FinanceAutobillRecordMapper;
import com.ktgj.flight.api.RemoteFlightService;
import com.ktgj.flight.api.domain.FlightOrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.FinanceAutobillExceptionMapper;
import com.ktgj.customer.domain.FinanceAutobillException;
import com.ktgj.customer.service.IFinanceAutobillExceptionService;

/**
 * 财务自动对账结果Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-13
 */
@Service
public class FinanceAutobillExceptionServiceImpl implements IFinanceAutobillExceptionService 
{
    @Autowired
    private FinanceAutobillExceptionMapper financeAutobillExceptionMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RemoteFlightService remoteFlightService;

    @Autowired
    private FinanceAutobillRecordMapper financeAutobillRecordMapper;

    /**
     * 查询财务自动对账结果
     * 
     * @param billExceptionId 财务自动对账结果ID
     * @return 财务自动对账结果
     */
    @Override
    public FinanceAutobillException selectFinanceAutobillExceptionById(Long billExceptionId)
    {
        return financeAutobillExceptionMapper.selectFinanceAutobillExceptionById(billExceptionId);
    }

    /**
     * 查询财务自动对账结果列表
     * 
     * @param financeAutobillException 财务自动对账结果
     * @return 财务自动对账结果
     */
    @Override
    public List<FinanceAutobillException> selectFinanceAutobillExceptionList(FinanceAutobillException financeAutobillException)
    {
        return financeAutobillExceptionMapper.selectFinanceAutobillExceptionList(financeAutobillException);
    }

    /**
     * 新增财务自动对账结果
     * 
     * @param financeAutobillException 财务自动对账结果
     * @return 结果
     */
    @Override
    public int insertFinanceAutobillException(FinanceAutobillException financeAutobillException)
    {
        return financeAutobillExceptionMapper.insertFinanceAutobillException(financeAutobillException);
    }

    /**
     * 修改财务自动对账结果
     * 
     * @param financeAutobillException 财务自动对账结果
     * @return 结果
     */
    @Override
    public int updateFinanceAutobillException(FinanceAutobillException financeAutobillException)
    {
        return financeAutobillExceptionMapper.updateFinanceAutobillException(financeAutobillException);
    }

    /**
     * 批量删除财务自动对账结果
     * 
     * @param billExceptionIds 需要删除的财务自动对账结果ID
     * @return 结果
     */
    @Override
    public int deleteFinanceAutobillExceptionByIds(Long[] billExceptionIds)
    {
        return financeAutobillExceptionMapper.deleteFinanceAutobillExceptionByIds(billExceptionIds);
    }

    /**
     * 删除财务自动对账结果信息
     * 
     * @param billExceptionId 财务自动对账结果ID
     * @return 结果
     */
    @Override
    public int deleteFinanceAutobillExceptionById(Long billExceptionId)
    {
        return financeAutobillExceptionMapper.deleteFinanceAutobillExceptionById(billExceptionId);
    }

    /**
     * 对账结果列表
     * */
    @Override
    public List<FinanceAutobillException> financeAutobillExceptionList(FinanceAutobillExceptionVo financeAutobillExceptionVo) {
        List<FinanceAutobillException> financeAutobillExceptions = financeAutobillExceptionMapper.financeAutobillExceptions(financeAutobillExceptionVo);
        if (financeAutobillExceptions.size() > 0){
            for (FinanceAutobillException financeAutobillException : financeAutobillExceptions) {
                if (financeAutobillException.getOperator() != null){
                    Employee employee = employeeMapper.employeeNameList(financeAutobillException.getOperator());
                    if (employee != null){
                        financeAutobillException.setOperatorName(employee.getEmployeeName());
                    }
                }
            }
        }
        return financeAutobillExceptions;
    }

    /**
     * 回填票号
     * */
    @Override
    public int backfillTicketNumber(BackfillTicketNumberVo backfillTicketNumberVo) throws Exception
    {
        FinanceAutobillException financeAutobillException = new FinanceAutobillException();
        financeAutobillException.setBillExceptionId(backfillTicketNumberVo.getId());
        financeAutobillException.setTicketNumber(backfillTicketNumberVo.getTicketNum());
        financeAutobillException.setRemark(backfillTicketNumberVo.getRemark());
        int rows = financeAutobillExceptionMapper.updateFinanceAutobillException(financeAutobillException);
        return rows;
//        if (backfillTicketNumberVo.getTicketNum() != null){
//            Map<String, Object> map = new HashMap<>();
//            map.put("ticketNumber", backfillTicketNumberVo.getTicketNum());
//            map.put("id", backfillTicketNumberVo.getId());
//            int i = remoteFlightService.backfillTicketNumber(map);
//            if (i <= 0){
//                throw new Exception("回填票号失败");
//            }
//        }
//        if (backfillTicketNumberVo.getRemark() != null){
//            Map<String, Object> map = new HashMap<>();
//            map.put("remark", backfillTicketNumberVo.getRemark());
//            map.put("id", backfillTicketNumberVo.getId());
//            int i = remoteFlightService.backfillRemarks(map);
//            if (i <= 0){
//                throw new Exception("回填备注失败");
//            }
//        }
//        return 1;
    }

    /**
     * 对账调整列表
     * */
    @Override
    public List<FinanceAutobillException> reconciliationAdjustment(AutobillExceptionVo autobillExceptionVo) {
        List<FinanceAutobillException> financeAutobillExceptionList = financeAutobillExceptionMapper.reconciliationAdjustment(autobillExceptionVo);
        if (financeAutobillExceptionList.size() > 0){
            for (FinanceAutobillException financeAutobillException : financeAutobillExceptionList) {
                if (financeAutobillException != null){
                    if (financeAutobillException.getBillTimeNumber() != null){
                        FinanceAutobillRecord financeAutobillRecord = financeAutobillRecordMapper.billDetails(financeAutobillException.getSummaryBillId());
                        if (financeAutobillRecord != null){
                            if (financeAutobillRecord.getReportType() != null){
                                if (financeAutobillRecord.getReportType().equals(FlightOrderConstants.Sales.getValue())){
                                    financeAutobillException.setDataStart("销售");
                                }
                                else if (financeAutobillRecord.getReportType().equals(FlightOrderConstants.Refund.getValue())){
                                    financeAutobillException.setDataStart("退票");
                                }
                                else if (financeAutobillRecord.getReportType().equals(FlightOrderConstants.SalesAndRefunds.getValue())){
                                    financeAutobillException.setDataStart("销售和退票");
                                }
                            }
                        }
                    }
                    if (financeAutobillException.getExceptionType() != null){
                        if (financeAutobillException.getExceptionType().equals(FlightOrderConstants.ReturnMoneyLess.getValue())){
                            financeAutobillException.setExceptionType("少收客人退票费");
                        }
                        else if (financeAutobillException.getExceptionType().equals(FlightOrderConstants.ReturnMoneyMore.getValue())){
                            financeAutobillException.setExceptionType("多收客人退票费");
                        }
                        else if (financeAutobillException.getExceptionType().equals(FlightOrderConstants.CrmLess.getValue())){
                            financeAutobillException.setExceptionType("CRM没有对方有");
                        }
                        else if (financeAutobillException.getExceptionType().equals(FlightOrderConstants.CrmMore.getValue())){
                            financeAutobillException.setExceptionType("CRM有对方没有");
                        }
                        else if (financeAutobillException.getExceptionType().equals(FlightOrderConstants.BuyMoneyLess.getValue())){
                            financeAutobillException.setExceptionType("少收客人票价");
                        }
                        else if (financeAutobillException.getExceptionType().equals(FlightOrderConstants.BuyMoneyMore.getValue())){
                            financeAutobillException.setExceptionType("多收客人票价");
                        }
                        else if (financeAutobillException.getExceptionType().equals(FlightOrderConstants.Normal.getValue())){
                            financeAutobillException.setExceptionType("正常");
                        }
                        else if (financeAutobillException.getExceptionType().equals(FlightOrderConstants.DuplicateTicketNumber.getValue())){
                            financeAutobillException.setExceptionType("票号重复");
                        }
                    }

                    if (financeAutobillException.getStatus() != null){
                        if (financeAutobillException.getStatus().equals("1")){
                            financeAutobillException.setStatus("待系统自动处理");
                        }
                        else if (financeAutobillException.getStatus().equals("2")){
                            financeAutobillException.setStatus("待人工处理");
                        }
                        else if (financeAutobillException.getStatus().equals("3")){
                            financeAutobillException.setStatus("处理完成");
                        }
                    }
                }
            }
        }
        return financeAutobillExceptionList;
    }

    /**
     * 修改状态为票号重复
     * */
    @Override
    public int ticketNumRepeat(Long id) {
        int i = financeAutobillExceptionMapper.ticketNumRepeat(id);
        return i;
    }
}
